<?php

/*增删改查图片信息*/
namespace app\member\controller\db;

use app\common\Timer;
use app\member\model\Image as ImageModel;
use app\member\model\Like;
use think\Controller;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\facade\Log;
use think\facade\Session;
use function Sodium\add;

class Image extends Controller
{


    /*首页显示的*/
    public function selectAll(){
        $data = new ImageModel();
        try {
            $data = $data->where("image_delete_time",null)
                ->where("image_show","true")
                ->order("image_onload_stamp desc")->paginate(16);
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
            Log::warning("查询数据库错误");
        }
        return $data;
    }

    /*同归类显示的*/
    public function selectByClass($classId){
        $data = new ImageModel();
        $class = new ClassList();
        $className = $class->selectIdByName($classId);
        try {
            $data = $data->where("image_class",$className)
                ->where("image_delete_time",null)
                ->order("image_onload_stamp desc")
                ->where("image_show","true")
                ->paginate(16);
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
            Log::warning("查询数据库错误");
        }
        return $data;
    }

    /*显示首榜*/
    public function selectFirst(){
        $data = new ImageModel();
        try {
            $data = $data->where("image_delete_time",null)
                ->order("image_like desc")->limit(10)->select();
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
            Log::warning("查询数据库错误");
        }
        return $data->toArray();
    }

    /*
     * $quantity int
     * $bySearch string
     * $forSort String
     * $sort  bool
     * */
    /*首页显示作者允许显示的*/
    public function selectByAuthorMain($author){
        $data = new ImageModel();
        try {
            $data = $data->where("image_delete_time",null)
                ->whereIn("image_member_name",$author)
                ->where("image_show","true")
                ->order("image_onload_stamp desc")
                ->paginate(12);
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
            Log::warning("查询数据库错误");
        }
        return $data;
    }

    /*私人管理*/
    public function selectByAuthor($author){
        $data = new ImageModel();
        try {
            $data = $data->where("image_delete_time",null)
                ->whereIn("image_member_name",$author)
                ->order("image_onload_stamp desc")
                ->paginate(10);
//   暂时不进行分页
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
            Log::warning("查询数据库错误");
        }
        return $data;
    }

    /*通过图片MD5查找图片*/
    public function selectByMd5($md5){
        $data = new ImageModel();
        try {
            $msg = $data->where("image_delete_time",null)->whereIn("image_md5",$md5)->find();
            if($msg !== null){
                return $msg->toArray();
            }else{
                return null;
            }
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
            Log::warning("查询数据库错误");
        }
        return null;
    }

    /*----图片入库-----*/
    public function addImage($data){
            $member = new ImageModel();
            $time = new Timer();
            return $member->save([
                    'image_md5'                 =>  $data['image_md5'],
                    'image_url'                 =>  $data['image_url'],
                    'image_member_name'         =>  $data['image_member_name'],
                    'image_class'               =>  $data['image_class'],       //类别
                    "image_content"             =>  $data["image_content"],
                    'image_onload_stamp'        =>  $time->getTime(),           //未设置
                    'image_show'                =>  $data['image_show'],        //显示
                    'image_download_count'      =>  0,                          //未设置
                ]);

    }

    /*------------------------软删除图片 start--------------------------*/
    public function softDeleteImage($md5){
        $time = new Timer();
        $image = new ImageModel();  /*模型*/

        try {
            $image = $image->where("image_md5", $md5)->find();
            $image->image_delete_time = $time->getTime();
            $image->save();
            $this->redirect("/user/upload");
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
            Log::write("软删除");
        }
        $this->redirect("/user/upload");
    }
    /*------------------------软删除图片 end--------------------------*/


    /*------------------------软删除图片撤回 start--------------------------*/
    public function softRecoverImage($md5){
        $image = new ImageModel();
        try {
            $image = $image->where("image_md5", $md5)->find();
            $image->image_delete_time = null;
            $image->save();
            $this->redirect("/user/upload");
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
            Log::write("软删除失败");
        }
        $this->redirect("/user/upload");
    }

    /*------------------------硬删除图片 （管理员用）start--------------------------*/
    public function AdminDeleteImage(){
        $image = new ImageModel();
        try {
            // TODO 可能有问题
            return $image->whereNotNull("image_deleted_time", null)->delete();
        } catch (\Exception $e) {
            Log::write("硬删除失败");
        }
        return "false";
    }


    public function DeleteImage($author){
        $image = new ImageModel();
            // TODO 可能有问题
        $data = $image->whereNotNull("image_delete_time")
            ->whereIn("image_member_name",$author)
            ->delete();
    }

    /*------------------------硬删除图片 end--------------------------*/


    /*------------------------回收站数据查询-------------------------*/
    public function deletedImage($author){
        $image = new ImageModel();
         $data = $image->whereNotNull("image_delete_time")->whereIn("image_member_name",$author)->order("image_onload_stamp desc")->select();
        return $data->toArray();
    }
    /*------------------------回收站-------------------------*/


    /*显示图片 start*/
    public function showImage($md5){
        $data = new ImageModel();
        $data =  $data->where("image_md5",$md5)->find();
        $data->image_show = "true";
        $data->save();

    }
    /*显示图片 end*/


    /*私有图片 start*/
    public function hiddenImage($md5){
        $data = new ImageModel();
        $data = $data->where("image_md5",$md5)->find();
        $data->image_show = null;
        $data->save();

    }
        /*显示图片 end*/
    /*私有图片 end*/

    /*--------------------------------------点赞----------------------------------*/

    public function likeIt($id){
        $image = new ImageModel();
        $like = new Like();
        $res = $like->where("like_member",Session::get("login"))
            ->where("like_image_md5", $id)
            ->find();
        $image = $image->where("image_md5", $id)->find();
        if(Session::get($id) && $res!==null){
            $image->image_like -= 1;
            $image->save();
            $res->delete();
            Session::delete($id);
            return false;
        }else{
            $image->image_like += 1;
            $image->save();
                $like->save([
                    "like_member" => Session::get("login"),
                    "like_image_md5"=>$id
                ]);
            Session::set($id,$id);
            return true;
        }
    }
    public function reviewMsg($id){
        $image = new ImageModel();
        $msg = $image->where("image_md5",$id)->find();
        if(json_decode($msg->image_review)!==null){
            return array_reverse(json_decode($msg->image_review));
        }
        return json_decode($msg->image_review);
    }

    public function reviewThis($id,$msg){
        $image = new ImageModel();
        $data = $this->reviewMsg($id);
        if($data === null){
            $data = array();
        }
        $arr2 = array("name"=>Session::get("login"),"msg"=>$msg);
        array_push($data,$arr2);
        $image->where('image_md5',$id)->update(['image_review'=>json_encode($data)]);
        return $data;
    }

}